﻿using System.Collections.ObjectModel;
using HMEngine;
using HMEngine.HMCameras;
using HMEngine.HMEffects;
using HMEngine.HMInputs;
using HMEngine.HMObjects;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace HMDemo {
    internal static class HMDemo {
        private static readonly HMGame game = new HMGame(800, 600);
        private static readonly HMQuad quad = new HMQuad("Content/Textures/hazymind", Color.White);
        private static readonly HMShader shader = new HMShader("HMEngineContent/Shaders/TransformColorTexture");
        private static readonly HMKeyboardDevice keyboard = new HMKeyboardDevice();
        private static readonly HMMouseDevice mouse = new HMMouseDevice();
        private static readonly HMGamePadDevice gamepad = new HMGamePadDevice(PlayerIndex.One);

        public static void Main() {
            HMInputManager.AddDevice("keyboard", keyboard);
            keyboard.OnKeyReleased += keyboard_OnKeyReleased;

            HMInputManager.AddDevice("mouse", mouse);
            mouse.OnMouseMoved += mouse_OnMouseMoved;
            mouse.OnMouseScrolled += mouse_OnMouseScrolled;

            HMInputManager.AddDevice("player1", gamepad);
            gamepad.OnButtonReleased += gamepad_OnButtonReleased;
            gamepad.OnJoystickMoved += gamepad_OnJoystickMoved;

            HMEffectManager.AddEffect("TCT", shader);
            quad.Shader = "TCT";

            HMObjectManager.AddObject("quad", quad);
            game.Run();
        }

        private static void keyboard_OnKeyReleased(Collection<Keys> keys) {
            if (keys.Contains(Keys.F)) {
                game.ToggleFullScreen();
            }
            if (keys.Contains(Keys.Escape)) {
                game.Exit();
            }
        }

        private static void mouse_OnMouseMoved(Vector2 move) {
            HMCameraManager.ActiveCamera.Revolve(new Vector3(1, 0, 0), move.Y * 0.01f);
            HMCameraManager.ActiveCamera.RevolveGlobal(new Vector3(0, 1, 0), move.X * 0.01f);
        }

        private static void mouse_OnMouseScrolled(int ticks) { HMCameraManager.ActiveCamera.Translate(new Vector3(0, 0, ticks * 0.01f)); }

        private static void gamepad_OnButtonReleased(Collection<HMGamePadButton> buttons) {
            if (buttons.Contains(HMGamePadButton.Back)) {
                game.Exit();
            }
        }

        private static void gamepad_OnJoystickMoved(Vector2 leftStick, Vector2 rightStick) {
            HMCameraManager.ActiveCamera.Revolve(new Vector3(1, 0, 0), leftStick.Y * 0.1f);
            HMCameraManager.ActiveCamera.RevolveGlobal(new Vector3(0, 1, 0), leftStick.X * 0.1f);
            HMCameraManager.ActiveCamera.Translate(new Vector3(0, 0, -rightStick.Y * 0.5f));
        }
    }
}